﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//搭载玩家来回移动的平台,不一定是TileMap
public class Platform_Transport : Platform_Movable
{
    private GameObject obj_player;
    private bool b_touchPlayer;         //是否与玩家紧贴

    private void Start()
    {
        obj_player = Character.Instance.gameObject;
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.gameObject.GetComponent<Character>() != null)
        {
            b_touchPlayer = false;
            obj_player.GetComponent<Rigidbody2D>().velocity += speed * new Vector2(Direction.x, 0);
        }
    }

    protected override void Move()
    {
        base.Move();
        if (b_touchPlayer)
            obj_player.transform.position += new Vector3(speed * Time.fixedDeltaTime * Direction.x, 0, 0);
    }

    protected override void OnTouchPlayer()
    {
        b_touchPlayer = true;
        if (!b_started)
        {
            b_started = true;
        }
    }

    protected override void OnTouchObstacle()
    {
        Direction *= -1;
    }
}
